#include "mpconfig.h"
#include "CTConfig.h"
#include "CTNull.h"

#define MAIN_SECTION    "Global"

// int型
#define GETINT(name, value) \
    value = cfg.GetInt(MAIN_SECTION, name, &iRet); \
	if (iRet) \
	{ \
		CT_THROW(("[%s]%s failed %d", MAIN_SECTION, name, iRet)); \
	}

// char数组
#define GETSTR(name, value) \
    s = cfg.GetString(MAIN_SECTION, name, &iRet); \
	if (iRet) \
	{ \
		CT_THROW(("[%s]%s failed %d", MAIN_SECTION, name, iRet)); \
	} \
    iRet = ct_snprintf(CT_STRSIZE(value), "%s", s.c_str()); \
    if (iRet < 0) \
    { \
        CT_THROW(("[%s]%s too long, over %d", MAIN_SECTION, name, sizeof(value))); \
    }

// string
#define GETSTRING(name, value) \
    value = cfg.GetString(MAIN_SECTION, name, &iRet); \
	if (iRet) \
	{ \
		CT_THROW(("[%s]%s failed %d", MAIN_SECTION, name, iRet)); \
	}

void CMpConfig::LoadConfig()
{
	CTConfig cfg;
    string s;
	int iRet = cfg.LoadConfig(FILE_CONFIG);
	if (iRet)
	{
		CT_THROW(("LoadConfig failed: %d", iRet));
	}

    GETSTR("FrontEndShmKey", FrontEndShmKey);
    //Ftok(CT_STRSIZE(FrontEndShmKey));

    GETINT("FrontEndShmSize", FrontEndShmSize);
    GETSTR("FrontEndSocket", FrontEndSocket);
    GETSTR("BackEndSocket", BackEndSocket);

	int iKey = (int)ftok(FrontEndShmKey, 0);
	if (iKey <= 0)
    {
        CT_THROW(("[%s]FrontEndShmKey(%s) invalid", MAIN_SECTION, FrontEndShmKey));
    }

    if (FrontEndShmSize <= 0 || (FrontEndShmSize % 8 != 0))
    {
        CT_THROW(("[%s]FrontEndShmSize(%d) must mod 8 = 0", MAIN_SECTION, FrontEndShmSize));
    }

    GETSTR("ProcessSocket", ProcessSocket);
    GETINT("ProcessNum", ProcessNum);
    GETINT("ProcessRecvSize", ProcessRecvSize);
    GETINT("ProcessSendSize", ProcessSendSize);
    GETINT("ProcessLoopNum", ProcessLoopNum);
    GETINT("ProcessTimeOut", ProcessTimeOut);

    if (strlen(ProcessSocket) == 0 ||
        ProcessNum <= 0 || ProcessRecvSize <= 0 || ProcessSendSize <= 0 ||
        ProcessLoopNum <= 0 || ProcessTimeOut <= 0)
    {
        CT_THROW(("[%s]Process config invalid", MAIN_SECTION));
    }

    /*
    GETSTR("DBHost", DBHost);
    GETSTR("DBUser", DBUser);
    GETSTR("DBPass", DBPass);
    int iPort = 0;
    GETINT("DBPort", iPort);
    DBPort = (unsigned short)(iPort & 0xFFFF);
    GETSTR("DBSocket", DBSocket);
    */
	GETSTR("SoFile", SoFile);
	GETSTR("SoConfigFile", SoConfigFile);

	LoadLogConfig();
}

void CMpConfig::ReloadConfig()
{
    LoadLogConfig();
}

void CMpConfig::LoadLogConfig()
{
	CTConfig cfg;
	int iRet = cfg.LoadConfig(FILE_CONFIG);
	if (iRet)
	{
		CT_THROW(("LoadConfig failed: %d", iRet));
	}

    int iLogFileNum = 0, iLogFileSize = 0;
	GETINT("LogFileNum", iLogFileNum);
	GETINT("LogFileSize", iLogFileSize);
	CT_NLOG->Init(ENamed, NULL, FILE_NORMALLOG, iLogFileSize, iLogFileNum);
	CT_ELOG->Init(ENamed, NULL, FILE_ERRORLOG, iLogFileSize, iLogFileNum);
	CT_STAT->Init(ENamed, NULL, FILE_STATLOG, iLogFileSize, iLogFileNum);
	CT_STAT->SetFormat(ETime|ENewLine);

	// 设置filter
    string str;
	GETSTRING("LogFormat", str)
	CT_NLOG->SetFormatByStr(str.c_str());
	CT_ELOG->SetFormatByStr(str.c_str());

	// 设置filter
	GETSTRING("LogFilter", str)
	CT_NLOG->SetFilterByStr(str.c_str());
	CT_ELOG->SetFilterByStr(str.c_str());

	CT_INFO(("LogFormat=%d LogFilter=%d", CT_NLOG->GetFormat(), CT_NLOG->GetFilter()));
}

#ifndef WIN32
    #include <sys/types.h>
    #include <sys/ipc.h>
#endif
void CMpConfig::Ftok(char* pszPathName, size_t iLen)
{
#ifndef WIN32
    key_t iKey = ftok(pszPathName, 0);
    if (iKey == -1)
    {
        CT_THROW(("ftok(%s) failed %d", pszPathName, CT_ERRNO));
    }

    ct_snprintf(pszPathName, iLen, "%d", iKey);
#endif
}
